// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	10/9/2014 11:58:10 AM				
// -----------------------------

#include "Game.h"

#include "Application.h"
#include "InputManager.h"
#include "FileManager.h"
#include "AudioManager.h"
#include "RenderManager.h"

#include "SceneLoader.h"
#include "Level.h"

Game::Game()
	: m_lpScene(nullptr)
{
}

Game::~Game()
{
	delete m_lpScene;
}

void Game::OnInit(Application* lpApp, GameConfig& config)
{
	// Construct the Caption
	TStringStream captionStream;
	captionStream << _T("Procgen ");
	#ifdef _DEBUG
	captionStream << _T("(Debug");
	#else
	captionStream << _T("(Release");
	#endif
	#ifdef _NOD2D
	captionStream << _T(" No D2D");
	#endif
	#ifdef _WIN64
	captionStream << _T(" x64)");
	#else
	captionStream << _T(" x86)");
	#endif

	// Save the Config
	config.Caption = captionStream.str();
	config.Width = 1024;
	config.Height = 768;
	config.Fullscreen = false;
	config.VSync = false;

	// Hide the Cursor
	lpApp->EnableCursor(false);
}
void Game::OnStart(Application* lpApp)
{
	GenerateScene(lpApp);
}
void Game::OnEnd(Application* lpApp)
{

}

void Game::OnUpdate(Application* lpApp, Float deltaTime)
{
	if (lpApp->GetInput()->IsKeyReleased(VK_ESCAPE))
		lpApp->Quit();

	if (lpApp->GetInput()->IsKeyPressed(VK_F1))
		lpApp->GetRenderer()->SetRenderState(RenderState::Default);
	if (lpApp->GetInput()->IsKeyPressed(VK_F2))
		lpApp->GetRenderer()->SetRenderState(RenderState::Wireframe);

	if (lpApp->GetInput()->IsKeyPressed(VK_F5))
		GenerateScene(lpApp);

	if (lpApp->GetInput()->IsKeyPressed(VK_F11))
		lpApp->ToggleFullscreen();

	if (lpApp->GetInput()->IsKeyPressed(VK_F10))
		lpApp->GetRenderer()->SaveScreenshot();

}
void Game::OnRender(Application* lpApp)
{

}

void Game::GenerateScene(Application* lpApp)
{
	if (m_lpScene)
		Log::WriteLine(_T("[INFO]\t\tRegenerating Scene."));

	// Create the Scene
	Scene* lpScene = new Level(_T("Level"));

	// Create the Scene Loader
	SceneLoader* lpLoader = new SceneLoader(_T("LevelLoader"), lpScene);
	lpLoader->Load(lpApp);
	lpApp->SetScene(lpLoader);

	if (m_lpScene)
		delete m_lpScene;
	m_lpScene = lpScene;
}